package meterMonitor.biz;

import meterMonitor.mapper.GatewayMapper;
import meterMonitor.mapper.MeterMapper;
import meterMonitor.model.Gateway;
import meterMonitor.model.Meter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GatewayBiz extends BaseBiz {

    @Autowired
    private GatewayMapper gatewayMapper;
    @Autowired
    private MeterMapper meterMapper;

    public Gateway queryOneByIp(String ip){
        return gatewayMapper.queryOneByIp(ip);
    }


    public void addGateway(Gateway gateway) {
        gatewayMapper.add(gateway);
    }

    public void updateGateway(Gateway gateway) {
        gatewayMapper.update(gateway);
    }

    public List<Gateway> query(Gateway gateway) {
        return gatewayMapper.query(gateway);
    }

    public void delete(Gateway gateway) {
        gatewayMapper.delete(gateway);
    }

    public List<Gateway> getAllForIndex() {
        List<Gateway> gateways = query(new Gateway());
        for (Gateway g : gateways) {
            int running = 0;
            int offline = 0;
            List<Meter> meters = meterMapper.queryByGatewayId(g.getGatewayId());
            g.setTotal(meters.size());
            for (Meter m : meters) {
                // 在线状态
                if (m.getStatus() == 10) {
                    running++;
                } else {
                    offline++;
                }
            }
            g.setRunning(running);
            g.setOffline(offline);
            g.setMeters(meters);
        }
        return gateways;
    }


}
